from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.


from rest_framework.views import APIView
from luffyapi_2.utils.response import APIResponse


# class TestView(APIView):
#     def get(self,request,*args,**kwargs):
#         dic={'name':'lqz'}
#         # print(dic['age'])
#         print('xxxxx')
#         return APIResponse()

'''
if request.method=="OPTIONS":
            #可以加*
            response["Access-Control-Allow-Headers"]="Content-Type"
        response["Access-Control-Allow-Origin"] = "http://localhost:8080"
        return response
'''
# class TestView(APIView):
#     def options(self, request, *args, **kwargs):
#         print('optionsssssss')
#         return APIResponse(headers={'Access-Control-Allow-Origin':'*'})
#
#     def post(self, request, *args, **kwargs):
#         # log.info('xxxxxx')  # 不要写print
#         dic = {'name': 'lqz'}
#         print('post.........')
#         return APIResponse(headers={'Access-Control-Allow-Origin': '*'})
#
#     def get(self, request, *args, **kwargs):
#         dic = {'name': 'lqz'}
#         # print(dic['age'])
#         print('xxxxx')
#         res = APIResponse()
#         res['Access-Control-Allow-Origin'] = '*'
#         return res

from django.shortcuts import render,HttpResponse
def test(request):
    print(request.method)
    res= HttpResponse('ok')
    '''
    考虑怎么把下面的放在中间件里面
    # 先把跨域的允许源设为所有
    '''

    res['Access-Control-Allow-Origin']='*'
    #如果是非简单请求，判断它是不是options，如果是走内容
    if request.method == "OPTIONS":
        # 可以加*
        res["Access-Control-Allow-Headers"] = "Content-Type"
    return res
